package org.vectrics.recruiter.struts.admin;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Set;

import org.apache.ecs.html.IMG;
import org.apache.ecs.html.Input;
import org.vectrics.SystemException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobCandidate;
import org.vectrics.recruiter.user.CandidateInfo;
import org.vectrics.recruiter.user.resume.email.EmailedResumeInfo;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;

public class EmailJobMatchSelectTag extends VectricsTagSupport {

	private static final long serialVersionUID = 3967254269652398296L;
	
	private String emailedResumeId = null;

	public EmailJobMatchSelectTag() {
		super();
	}

	protected String makeStartTag() {
		EmailedResumeInfo emailedResumeInfo = RecruitServiceLocator.getRecruitUserService().findEmailedResume(new Long(this.getEmailedResumeId()));
		if (emailedResumeInfo.getPerson() == null)
			throw new SystemException("Person not selected befor attempt to select job");

		if (emailedResumeInfo.getPerson().getId() == null)
			throw new SystemException("Selected person not saved before attempt to select job");

		Person person = emailedResumeInfo.getPerson();
		CandidateInfo candidateInfo = RecruitServiceLocator.getRecruitUserService().findCandidateInfo(person);
		if (candidateInfo == null) {
			throw new SystemException("CandidateInfo not created for person, this needs to be done before assigning jobs to emailed resume, personId = " + person.getId());
		}

		Set jobs = (Set)this.getRequest().getAttribute("jobs");
		
		if (jobs == null) {
			throw new SystemException("Could not find request attribute 'jobs'");
		}
		
		Table table = new Table();
		table.setWidth("100%");
 
//		makeSepLine(table);
		
		Iterator iter = jobs.iterator();
		while (iter.hasNext()) {
			Job job = (Job)iter.next();
			job = RecruitServiceLocator.getJobProvider().findJob(job.getId());
			JobCandidate jobCandidate = job.findJobCandidate(candidateInfo);
			makeSepLine(table);
			
			Row row = table.addRow();
			row.setDefaultCellFontClass("stdFont");
			Cell cellCheck = row.addCell();
			cellCheck.setWidth(0);
			cellCheck.setVAlign("top");

			if (jobCandidate == null)  {
				Input input = new Input();
				input.setType(Input.CHECKBOX);
				input.setName("job" + job.getId().toString());
				input.setChecked(false);
				if (job != null) {
					input.setChecked(true);
				}
				input.setValue(job.getId().toString());
				cellCheck.addElement(input);
			} else {
				cellCheck.addText("stdFont", this.getPhrase("phrase.person.is.candidate"));
			}

			Cell cellA = row.addCell();
			cellA.setWidth("100%");
			cellA.setVAlign("top");

			Table table1 = new Table();
			table1.setWidth("95%");
			cellA.addElement(table1);
			addNameValue(table1, "prompt.job.company", job.getCompany().getName());
			addNameValue(table1, "prompt.job.reference.id", job.getReferenceId());
			addNameValue(table1, "prompt.job.title", job.getTitle());
			addNameValue(table1, "prompt.job.status", job.getCurrentStatus().getDescription());

			SimpleDateFormat dateFormat = CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale());
			if (job.getDatePosted() != null) {
				addNameValue(table1, "prompt.job.date.posted", dateFormat.format(job.getDatePosted()));
			} else {
				addNameValue(table1, "prompt.job.date.posted", null);
			}
		}
		makeSepLine(table);
		return table.toString();
	}

	private void makeSepLine(Table table) {
		Row row = table.addRow();
		Cell cell = row.addCell();
		cell.setColSpan(3);
		IMG sepLine = new IMG();
		sepLine.setWidth("100%");
		sepLine.setHeight(1);
		sepLine.setSrc(this.getRequest().getContextPath() + "/graphics/dk_slate_dot.gif");
		cell.addElement(sepLine);
	}
	
	
	private void addNameValue(Table table, String captionCode, String value) {
		Row row = table.addRow();
		row.setDefaultCellFontClass("stdFont");
		Cell captionCell = row.addCell();
		captionCell.setVAlign("top");
		String caption = this.getPhrase(captionCode);
		captionCell.addText(caption);

		Cell valueCell = row.addCell();
		valueCell.setVAlign("top");
		Table innerBorderTable = new BorderTable();
		innerBorderTable.setWidth("100%");
		Row innerBorderRow = innerBorderTable.addRow();
		Cell innerBorderCell = innerBorderRow.addCell();
		valueCell.addElement(innerBorderTable);
		innerBorderCell.setClass("readOnlyBg");
		if ((value == null) || (value.trim().length() == 0)) {
			innerBorderCell.addElement(TextHelper.getFont("stdFont", this.getPhrase("not.given")));
		} else {
			innerBorderCell.addElement(TextHelper.getFont("stdFont", value));
		}
	}

	/**
	 * @return Returns the emailedResumeId.
	 */
	public String getEmailedResumeId() {
		return emailedResumeId;
	}

	/**
	 * @param emailedResumeId The emailedResumeId to set.
	 */
	public void setEmailedResumeId(String emailedResumeId) {
		this.emailedResumeId = emailedResumeId;
	}

}
